<?php
/**
 *  地区
 */
namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Region;
use Illuminate\Http\Request;

class AreaController extends Controller
{

    /**
     * 获取区域
     * URL POST:/api/common/region
     * @param int pid 父级ID
     * @return mixed
     */
    public function region()
    {
        $pid = request()->post('pid', 0);
        if (!is_numeric($pid)) {
            return $this->error('参数错误！');
        }
        $list = Region::getByPid($pid);
        $data['list'] = $list;
        return $this->success('查询成功',$data);
    }

    /**
     * 获取市
     * URL POST:/api/common/region/city
     * @return mixed
     */
    public function city()
    {
        $list = Region::where([['level', 2], ['status', 1]])->get(['id', 'name', 'short_name', 'initials'])->toArray();
        $data['list'] = groupByInitials($list, 'initials');
        // 热门城市
        $hot_city = Region::where([ ['status', 1],['is_hot',1]])->get(['id', 'name', 'short_name', 'initials'])->toArray();
        $data['hot_city'] = $hot_city;
        return $this->success('查询成功',$data);
    }

    /**
     * 获取城市ID
     * URL POST:/api/common/region/get_id
     * @param string city_cn 城市名称
     * @param int level
     * @return mixed
     */
    public function getByName()
    {
        $city_cn = request()->post('city_cn', '');
        $level = \request()->post('level','');
        $city = Region::getByNameId($city_cn, $level);
//        $data['city'] = $city ?: 0;
        return $this->success('查询成功',$city);
    }

    /**
     * 通过id获取名称
     * URL POST:/api/common/region/get_name
     * @return mixed
     */
    public function getById()
    {
        $city_id = request()->post('city_id', 0);
        if (!$city_id) {
            return $this->error('城市参数是必须的');
        }
        $name = Region::getByIdName($city_id);
        return $this->success('查询成功',['name' => $name]);
    }

    /**
     * 根据省级ID查询所有的区级城市
     * URL POST:/api/common/region/getArea
     * @param Request $request
     * @param int pid 父级ID
     * @return mixed
     */
    public function getAreaByPid(Request $request){
        $params = $request->only(['pid']);
        if (!array_key_exists('pid',$params) || empty($params['pid'])) {
            return $this->error('省ID是必须的');
        }
        $region = Region::find($params['pid']);
        if ($region->level == 1) { // 市
            // 查询省对应的所有市ID
            $data = Region::getByPid($params['pid']);
            $cityIds = array_column($data,'id');
        }else{
            $cityIds = [$params['pid']];
        }
        // 根据市查询区级城市
        $param['whereIn'][] = ['parent_id',$cityIds];
        $param['field'] = ['id', 'name', 'short_name','level'];
        $param['orderBy'] = ['id asc'];
        $result = Region::getAll($param);
        return $this->success('查询成功',$result);
    }

    /**
     * 根据关键字查询城市
     * URL POST:/api/common/region/search
     * @param Request $request
     * @param string keyword 关键字
     * @return mixed
     */
    public function search(Request $request){
        $params = $request->only(['keyword']);
        if (!array_key_exists('keyword',$params) || empty($params['keyword'])) {
            return $this->error('缺少关键字');
        }
        $params['where'][] = ['status',1]; // 启用
        $params['where'][] = ['level','=',2];
        $params['where'][] = ['name','like','%'.$params['keyword'].'%'];
        $params['field'] = ['id','name','short_name','level'];
        $params['orderBy'] = ['id asc'];
        $result = Region::getAll($params);
        return $this->success('查询成功',$result);
    }
}